import Cookies from "js-cookie";
const prefix = "vivo_feeds_mobile_";
export const CookiesStorage = {
  getItem: (key: string) => {
    const value: any = Cookies.get(prefix + key);
    if (isEmptyType(value)) return "";
    if (isJSONString(value) && isEnableParse(value)) {
      return JSON.parse(value);
    }
    return value;
  },
  setItem: (key: string, value: any, duration = 8 * 60 * 60 * 1000) => {
    if (isEmptyType(value)) return;
    if (isObject(value) || isArray(value)) {
      value = JSON.stringify(value);
    }
    const expires = new Date(Date.now() + duration);
    Cookies.set(prefix + key, value, { expires });
  },
  removeItem: (key: string) => {
    Cookies.remove(prefix + key);
  },
};
export const SessionStorage = {
  getItem: (key: string) => {
    const value: any = sessionStorage.getItem(prefix + key);
    if (isEmptyType(value)) return "";
    if (isJSONString(value) && isEnableParse(value)) {
      return JSON.parse(value);
    }
    return value;
  },
  setItem: (key: string, value: any) => {
    if (isEmptyType(value)) return;
    if (isObject(value) || isArray(value)) {
      sessionStorage.setItem(prefix + key, JSON.stringify(value));
      return;
    }
    sessionStorage.setItem(prefix + key, value);
  },
  removeItem: (key: string) => {
    sessionStorage.removeItem(prefix + key);
  },
  clear() {
    sessionStorage.clear();
  },
  key: (index: number) => {
    return sessionStorage.key(index);
  },
  length: sessionStorage.length,
};
export const LocalStorage = {
  getItem: (key: string) => {
    const value: any = localStorage.getItem(prefix + key);
    if (isEmptyType(value)) return "";
    if (isJSONString(value) && isEnableParse(value)) {
      return JSON.parse(value);
    }
    return value;
  },
  setItem: (key: string, value: any) => {
    if (isEmptyType(value)) return;
    if (isObject(value) || isArray(value)) {
      localStorage.setItem(prefix + key, JSON.stringify(value));
      return;
    }
    localStorage.setItem(prefix + key, value);
  },
  removeItem: (key: string) => {
    localStorage.removeItem(prefix + key);
  },
  clear() {
    localStorage.clear();
  },
  key: (index: number) => {
    return localStorage.key(index);
  },
  length: localStorage.length,
};
